package org.lmind.osgi.service.cxf.internal;

import javax.xml.ws.Endpoint;

import org.lmind.osgi.service.api.ws.WebServicePublisher;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;

public class HttpServiceTracker extends ServiceTracker implements WebServicePublisher {

	public HttpServiceTracker(BundleContext context) {
		super(context, HttpService.class.getName(), null);
	}

	@Override
	public Object addingService(ServiceReference reference) {
		HttpService http = (HttpService)super.addingService(reference);
		try {
			http.registerServlet("/", new CXFServlet(), null, null);
			context.registerService(WebServicePublisher.class, this, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return http;
	}

	@Override
	public void publish(String address, Object implementor) {
		Endpoint.publish(address, implementor);
	}

}
